package com.goodving.guli.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventUserInfoUpdate;
import com.goodving.guli.bt.eventbus.EventUserSelected;
import com.goodving.guli.bt.eventbus.EventWeightWithUser;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.bt.weight.family.WeightChooseUserActivity;
import com.goodving.guli.fragment.SettingDateFragment;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.BitmapDescriptorFactory;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/4.
 */
public class RecordWeightActivity extends SystemBarTintActivity {
    private static final String TAG = "RecordWeightActivity";
    private Context mContext;
    private EditText weightEditText;
    private TextView weightDate;
    private TextView weightName;
    private TextView weightUnit;
    private RelativeLayout weightLayout;
    private RelativeLayout dateLayout;
    private RelativeLayout userLayout;
    private Button confirmBut;
    private Button cancelBut;
    private String time;
    private DecimalFormat mDecimalFormat;
    private int uid;
    private boolean f11170o;

    public RecordWeightActivity() {
        this.mContext = this;
        this.uid = -1;
        this.f11170o = false;
    }

    class WeightTextWatcher implements TextWatcher {
        EditText mEditText;
        final /* synthetic */ RecordWeightActivity recordWeightActivity;
        private boolean f11534c;

        public WeightTextWatcher(RecordWeightActivity recordWeightActivity, EditText editText) {
            this.recordWeightActivity = recordWeightActivity;
            this.f11534c = false;
            this.mEditText = editText;
        }

        public void afterTextChanged(Editable editable) {
            if (!this.f11534c) {
                String obj;
                boolean z;
                String obj2 = editable.toString();
                if (obj2.isEmpty()) {
                    this.recordWeightActivity.confirmBut.setClickable(false);
                    this.recordWeightActivity.confirmBut.setTextColor(this.recordWeightActivity.mContext.getResources().getColor(R.color.disable_text_color_dark));
                } else {
                    this.recordWeightActivity.confirmBut.setClickable(true);
                    this.recordWeightActivity.confirmBut.setTextColor(this.recordWeightActivity.mContext.getResources().getColor(R.color.content_color));
                }
                this.f11534c = true;
                int length = obj2.length() - 1;
                while (length >= 0) {
                    if ('.' == obj2.charAt(length) && length == obj2.length() - 3) {
                        obj2 = obj2.substring(0, length + 2);
                        if (obj2.endsWith(".")) {
                            obj2 = obj2.substring(0, length + 1);
                        }
                        C3247b.makeText(this.recordWeightActivity.mContext, this.recordWeightActivity.mContext.getResources().getText(R.string.only_one_decimal), Toast.LENGTH_SHORT).show();
                        obj = obj2;
                        z = true;
                        if (z) {
                            this.mEditText.setText(obj);
                            this.mEditText.setSelection(obj.length());
                        }
                        this.f11534c = false;
                    }
                    length--;
                }
                obj = obj2;
                z = false;
                if (z) {
                    this.mEditText.setText(obj);
                    this.mEditText.setSelection(obj.length());
                }
                this.f11534c = false;
            }
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }
    }

    class UserLayoutOnClickListener implements View.OnClickListener {
        final /* synthetic */ RecordWeightActivity recordWeightActivity;

        UserLayoutOnClickListener(RecordWeightActivity recordWeightActivity) {
            this.recordWeightActivity = recordWeightActivity;
        }

        public void onClick(View view) {
            this.recordWeightActivity.chooseUser();
        }
    }

    class DateLayoutOnClickListener implements View.OnClickListener {
        final /* synthetic */ RecordWeightActivity recordWeightActivity;

        DateLayoutOnClickListener(RecordWeightActivity recordWeightActivity) {
            this.recordWeightActivity = recordWeightActivity;
        }

        public void onClick(View view) {
            this.recordWeightActivity.settingDate();
        }
    }

    class WeightLayoutOnClickListener implements View.OnClickListener {
        final /* synthetic */ RecordWeightActivity recordWeightActivity;

        WeightLayoutOnClickListener(RecordWeightActivity recordWeightActivity) {
            this.recordWeightActivity = recordWeightActivity;
        }

        public void onClick(View view) {
        }
    }

    class ConfirmOnClickListener implements View.OnClickListener {
        final /* synthetic */ RecordWeightActivity recordWeightActivity;

        ConfirmOnClickListener(RecordWeightActivity recordWeightActivity) {
            this.recordWeightActivity = recordWeightActivity;
        }

        public void onClick(View view) {
            this.recordWeightActivity.onConfirm();
        }
    }

    class CancelOnClickListener implements View.OnClickListener {
        final /* synthetic */ RecordWeightActivity recordWeightActivity;

        CancelOnClickListener(RecordWeightActivity recordWeightActivity) {
            this.recordWeightActivity = recordWeightActivity;
        }

        public void onClick(View view) {
            this.recordWeightActivity.finish();
        }
    }

    private void initView() {
        this.weightEditText = (EditText) findViewById(R.id.manual_weight_edittext);
        this.weightEditText.addTextChangedListener(new WeightTextWatcher(this, this.weightEditText));
        this.weightEditText.setFocusable(true);
        this.weightEditText.setFocusableInTouchMode(true);
        this.weightEditText.requestFocus();
        ((InputMethodManager) this.weightEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(this.weightEditText, 0);
        this.weightDate = (TextView) findViewById(R.id.manual_weight_date);
        this.weightName = (TextView) findViewById(R.id.manual_weight_name);
        this.weightName.setText(UserInfoManager.getInstance().getInfoByUid(this.uid).name);
        this.weightUnit = (TextView) findViewById(R.id.manual_weight_unit);
        this.weightUnit.setText(WeightUtil.m15698a(this.mContext, Keeper.readPersonInfo().miliConfig.weightUnit));
        this.weightLayout = (RelativeLayout) findViewById(R.id.weight_layout);
        this.dateLayout = (RelativeLayout) findViewById(R.id.date_layout);
        this.userLayout = (RelativeLayout) findViewById(R.id.user_layout);
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        this.mDecimalFormat = new DecimalFormat("00");
        int hours = new Date().getHours();
        int minutes = new Date().getMinutes();
        if (Utils.m8103x(this.mContext)) {
            this.weightDate.setText(simpleDateFormat.format(new Date()) + " " + this.mDecimalFormat.format((long) hours) + C4220a.ci + this.mDecimalFormat.format((long) minutes));
        } else if (hours >= 12) {
            this.weightDate.setText(simpleDateFormat.format(new Date()) + " " + this.mDecimalFormat.format((long) (hours - 12)) + C4220a.ci + this.mDecimalFormat.format((long) minutes) + " " + getString(R.string.pm));
        } else {
            this.weightDate.setText(simpleDateFormat.format(new Date()) + " " + this.mDecimalFormat.format((long) hours) + C4220a.ci + this.mDecimalFormat.format((long) minutes) + " " + getString(R.string.am));
        }
        this.weightLayout.setOnClickListener(new WeightLayoutOnClickListener(this));
        this.dateLayout.setOnClickListener(new DateLayoutOnClickListener(this));
        this.userLayout.setOnClickListener(new UserLayoutOnClickListener(this));
        this.confirmBut = (Button) findViewById(R.id.confirm);
        this.confirmBut.setOnClickListener(new ConfirmOnClickListener(this));
        this.cancelBut = (Button) findViewById(R.id.cancel);
        this.cancelBut.setOnClickListener(new CancelOnClickListener(this));
        String obj = this.weightEditText.getText().toString();
        if (obj == null || obj.isEmpty()) {
            this.confirmBut.setClickable(false);
            this.confirmBut.setTextColor(this.mContext.getResources().getColor(R.color.disable_text_color_dark));
        }
    }

    class C2045q implements SettingDateFragment.C1971F {
        final /* synthetic */ RecordWeightActivity recordWeightActivity;

        C2045q(RecordWeightActivity recordWeightActivity) {
            this.recordWeightActivity = recordWeightActivity;
        }

        public void m15553a() {
            this.recordWeightActivity.f11170o = false;
        }
    }

    class C2046r implements SettingDateFragment.C1970E {
        final /* synthetic */ RecordWeightActivity recordWeightActivity;

        C2046r(RecordWeightActivity recordWeightActivity) {
            this.recordWeightActivity = recordWeightActivity;
        }

        public void m15552a(int i, int i2, int i3, int i4, int i5) {
            if (Utils.m8103x(this.recordWeightActivity.mContext)) {
                this.recordWeightActivity.weightDate.setText(i + "-" + this.recordWeightActivity.mDecimalFormat.format((long) i2) + "-" + this.recordWeightActivity.mDecimalFormat.format((long) i3) + " " + this.recordWeightActivity.mDecimalFormat.format((long) i4) + C4220a.ci + this.recordWeightActivity.mDecimalFormat.format((long) i5));
            } else if (i4 >= 12) {
                this.recordWeightActivity.weightDate.setText(i + "-" + this.recordWeightActivity.mDecimalFormat.format((long) i2) + "-" + this.recordWeightActivity.mDecimalFormat.format((long) i3) + " " + this.recordWeightActivity.mDecimalFormat.format((long) (i4 - 12)) + C4220a.ci + this.recordWeightActivity.mDecimalFormat.format((long) i5) + " " + this.recordWeightActivity.getString(R.string.pm));
            } else {
                this.recordWeightActivity.weightDate.setText(i + "-" + this.recordWeightActivity.mDecimalFormat.format((long) i2) + "-" + this.recordWeightActivity.mDecimalFormat.format((long) i3) + " " + this.recordWeightActivity.mDecimalFormat.format((long) i4) + C4220a.ci + this.recordWeightActivity.mDecimalFormat.format((long) i5) + " " + this.recordWeightActivity.getString(R.string.am));
            }
            String str = i + "-" + this.recordWeightActivity.mDecimalFormat.format((long) i2) + "-" + this.recordWeightActivity.mDecimalFormat.format((long) i3) + " " + this.recordWeightActivity.mDecimalFormat.format((long) i4) + C4220a.ci + this.recordWeightActivity.mDecimalFormat.format((long) i5) + ":00";
            try {
                this.recordWeightActivity.time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str).getTime() + "";
                GuliLog.tracePrintInfo(TAG, "time str " + this.recordWeightActivity.time);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void settingDate() { // m15626b
        if (!this.f11170o) {
            this.f11170o = true;
            SettingDateFragment settingDateFragment = (SettingDateFragment) Fragment.instantiate(this.mContext, SettingDateFragment.class.getName());
            settingDateFragment.m15934a(new C2045q(this));
            settingDateFragment.m15933a(new C2046r(this));
            FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
            beginTransaction.addToBackStack(null);
            settingDateFragment.show(beginTransaction, null);
        }
    }

    private void chooseUser() {
        if (Utils.m8055c((Activity) this.mContext)) {
            this.mContext.startActivity(new Intent(this.mContext, WeightChooseUserActivity.class));
        }
    }

    private void onConfirm() {
        float f = BitmapDescriptorFactory.HUE_YELLOW;
        long currentTimeMillis = System.currentTimeMillis();
        if (!(this.time == null || this.time.isEmpty())) {
            currentTimeMillis = Long.parseLong(this.time);
        }
        String obj = this.weightEditText.getText().toString();
        if (!(obj == null || obj.isEmpty())) {
            try {
                f = WeightUtil.m15689a(WeightUtil.m15716c(Float.parseFloat(obj), 1), Keeper.readPersonInfo().miliConfig.weightUnit);
                if (f < 3.0f || f > 634.9f) {
                    C3247b.m22930a(this.mContext, getString(R.string.weight_not_in_range), 0).show();
                    return;
                }
            } catch (Exception e) {
                GuliLog.tracePrintInfo(TAG, "weight is invalid");
                C3247b.m22930a(this.mContext, getString(R.string.please_input_correct_weight), 0).show();
                return;
            }
        }
        UserInfo a = UserInfoManager.getInstance().getInfoByUid(this.uid);
        ScaleWeightInfo scaleWeightInfo = new ScaleWeightInfo();
        scaleWeightInfo.m25488b(WeightUtil.m15706b(f, Keeper.readPersonInfo().miliConfig.weightUnit));
        scaleWeightInfo.setUnitType(Keeper.readPersonInfo().miliConfig.weightUnit);
        scaleWeightInfo.setStable(true);
        scaleWeightInfo.setFinish(false);
        scaleWeightInfo.setDate(currentTimeMillis);
        scaleWeightInfo.m25490c(f);
        scaleWeightInfo.m25482a(WeightUtil.m15693a(a.height, Birthday.fromStr(a.birthday).getAge(), f));
        WeightInfo weightInfo = new WeightInfo();
        weightInfo.uid = a.uid;
        weightInfo.timestamp = scaleWeightInfo.m25494e();
        weightInfo.weight = f;
        weightInfo.type = 1;
        long e2 = scaleWeightInfo.m25494e();
        while (WeightInfoManager.getInstance().m15752a(e2)) {
            GuliLog.tracePrintInfo(TAG, "time exists " + e2);
            e2++;
        }
        GuliLog.tracePrintInfo(TAG, "save time is " + e2);
        weightInfo.timestamp = e2;
        scaleWeightInfo.setDate(e2);
        WeightInfoManager.getInstance().m15749a(weightInfo);
        EventBus.getDefault().post(new EventWeightWithUser(scaleWeightInfo, a.uid));
        EventBus.getDefault().post(new EventUserInfoUpdate(a));
        C0668a.m8128a(this.mContext, C0668a.gv, C3657a.f18328f + currentTimeMillis);
        finish();
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_record_weight);
        if (getIntent() != null) {
            this.uid = getIntent().getIntExtra(WeightGoalSetActivity.f11178a, -1);
            if (this.uid == 0) {
                this.uid = -1;
            }
        }
        initView();
        EventBus.getDefault().register(this);
    }

    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEvent(EventUserSelected eventUserSelected) {
        this.uid = eventUserSelected.uid;
        this.weightName.setText(UserInfoManager.getInstance().getInfoByUid(this.uid).name);
    }
}
